From 8350cd1734d560409437e0e9292ccb90477a5c31 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sun, 22 Mar 2015 12:25:54 -0700 Subject: [PATCH] Fix `cargo doc` Both tweak how deep documentation is compiled as well as fixing up the dependencies for a documented target. --- src/bin/doc.rs | 5 ++-- src/cargo/ops/cargo_compile.rs | 9 +++++++ src/cargo/ops/cargo_doc.rs | 1 - src/cargo/ops/cargo_rustc/context.rs | 39 ++++++++++++++++++++++++++-- src/cargo/ops/cargo_rustc/mod.rs | 1 + 5 files changed, 50 insertions(+), 5 deletions(-) diff --git a/src/bin/doc.rs b/src/bin/doc.rs index 65314fef2..6524ff52d 100644 --- a/src/bin/doc.rs +++ b/src/bin/doc.rs @@ -46,7 +46,6 @@ pub fn execute(options: Options, config: &Config) -> CliResult> { let root = try!(find_root_manifest_for_cwd(options.flag_manifest_path)); let mut doc_opts = ops::DocOptions { - all: !options.flag_no_deps, open_result: options.flag_open, compile_opts: ops::CompileOptions { config: config, @@ -58,7 +57,9 @@ pub fn execute(options: Options, config: &Config) -> CliResult> { exec_engine: None, filter: ops::CompileFilter::Everything, release: false, - mode: ops::CompileMode::Build, + mode: ops::CompileMode::Doc { + deps: !options.flag_no_deps, + }, }, }; diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index d74fa59b8..e2aec36b4 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -66,6 +66,7 @@ pub enum CompileMode { Test, Build, Bench, + Doc { deps: bool }, } pub enum CompileFilter<'a> { @@ -168,6 +169,9 @@ pub fn compile_pkg(package: &Package, options: &CompileOptions) let mut build_config = try!(scrape_build_config(config, jobs, target)); build_config.exec_engine = exec_engine.clone(); build_config.release = release; + if let CompileMode::Doc { deps } = mode { + build_config.doc_all = deps; + } try!(ops::compile_targets(&targets, to_build, &PackageSet::new(&packages), @@ -213,6 +217,7 @@ fn generate_targets<'a>(pkg: &'a Package, CompileMode::Test => test, CompileMode::Bench => &profiles.bench, CompileMode::Build => build, + CompileMode::Doc { .. } => &profiles.doc, }; return match *filter { CompileFilter::Everything => { @@ -243,6 +248,10 @@ fn generate_targets<'a>(pkg: &'a Package, t.is_bin() || t.is_lib() }).map(|t| (t, profile)).collect()) } + CompileMode::Doc { .. } => { + Ok(pkg.targets().iter().filter(|t| t.documented()) + .map(|t| (t, profile)).collect()) + } } } CompileFilter::Only { lib, bins, examples, tests, benches } => { diff --git a/src/cargo/ops/cargo_doc.rs b/src/cargo/ops/cargo_doc.rs index 884599012..fdfe8e3f6 100644 --- a/src/cargo/ops/cargo_doc.rs +++ b/src/cargo/ops/cargo_doc.rs @@ -10,7 +10,6 @@ use sources::PathSource; use util::{CargoResult, human}; pub struct DocOptions<'a, 'b: 'a> { - pub all: bool, pub open_result: bool, pub compile_opts: ops::CompileOptions<'a, 'b>, } diff --git a/src/cargo/ops/cargo_rustc/context.rs b/src/cargo/ops/cargo_rustc/context.rs index 22d9dc01c..70ccd88ff 100644 --- a/src/cargo/ops/cargo_rustc/context.rs +++ b/src/cargo/ops/cargo_rustc/context.rs @@ -34,7 +34,6 @@ pub struct Context<'a, 'b: 'a> { pub exec_engine: Arc>, pub fingerprints: HashMap<(&'a PackageId, &'a Target, &'a Profile, Kind), Fingerprint>, - pub initialized: HashSet<&'a PackageId>, pub compiled: HashSet<(&'a PackageId, &'a Target, &'a Profile)>, pub build_config: BuildConfig, @@ -92,7 +91,6 @@ impl<'a, 'b: 'a> Context<'a, 'b> { fingerprints: HashMap::new(), profiles: profiles, compiled: HashSet::new(), - initialized: HashSet::new(), }) } @@ -331,6 +329,9 @@ impl<'a, 'b: 'a> Context<'a, 'b> { pub fn dep_targets(&self, pkg: &Package, target: &Target, profile: &Profile) -> Vec<(&'a Package, &'a Target, &'a Profile)> { + if profile.doc { + return self.doc_deps(pkg); + } let deps = match self.resolve.deps(pkg.package_id()) { None => return Vec::new(), Some(deps) => deps, @@ -384,6 +385,40 @@ impl<'a, 'b: 'a> Context<'a, 'b> { return ret } + /// Returns the dependencies necessary to document a package + fn doc_deps(&self, pkg: &Package) + -> Vec<(&'a Package, &'a Target, &'a Profile)> { + let deps = self.resolve.deps(pkg.package_id()).into_iter(); + let deps = deps.flat_map(|a| a).map(|id| { + self.get_package(id) + }).filter(|dep| { + pkg.dependencies().iter().find(|d| { + d.name() == dep.name() + }).unwrap().is_transitive() + }).filter_map(|dep| { + dep.targets().iter().find(|t| t.is_lib()).map(|t| (dep, t)) + }); + + // To document a library, we depend on dependencies actually being + // built. If we're documenting *all* libraries, then we also depend on + // the documentation of the library being built. + let mut ret = Vec::new(); + for (dep, lib) in deps { + ret.push((dep, lib, self.lib_profile(dep.package_id()))); + if self.build_config.doc_all { + ret.push((dep, lib, &self.profiles.doc)); + } + } + + // Be sure to build/run the build script for documented libraries as + // well + let pkg = self.get_package(pkg.package_id()); + if let Some(t) = pkg.targets().iter().find(|t| t.is_custom_build()) { + ret.push((pkg, t, self.build_script_profile(pkg.package_id()))); + } + return ret + } + /// Gets a package for the given package id. pub fn get_package(&self, id: &PackageId) -> &'a Package { self.package_set.iter() diff --git a/src/cargo/ops/cargo_rustc/mod.rs b/src/cargo/ops/cargo_rustc/mod.rs index aba4c6262..d2ccaeb81 100644 --- a/src/cargo/ops/cargo_rustc/mod.rs +++ b/src/cargo/ops/cargo_rustc/mod.rs @@ -42,6 +42,7 @@ pub struct BuildConfig { pub requested_target: Option, pub exec_engine: Option>>, pub release: bool, + pub doc_all: bool, } #[derive(Clone, Default)] -- 2.30.2